<?php
namespace app\admin\controller;
use app\service\repairService;
use printing\printDemo;
use think\facade\Db;
use think\facade\View;

class Repair extends Common{
    //报修类型
    public function typeList(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复        
            $info = (new repairService())->typeName($data['name']);
            if($info){
                $this->error('类型已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new repairService())->typeAdd($data);
            if($result){
                $this->success('添加类型成功');
            }else{
                $this->error('添加类型失败');
            }
        }else{
            $list = (new repairService())->typeList();
            $number = count($list);
            View::assign('number',$number);
            View::assign('list',$list);
            return View::fetch('type_list'); 
        }
    }
    public function typeDel(){
        $id = request()->param('id');
        //判断是否存在该数据
		$info = (new repairService())->typeId($id);
		if(empty($info)){
			$this->error('该类型不存在');
		}
		$result = (new repairService())->typeDel($id);
        if($result){
            $this->success('删除类型成功');
		}else{
            $this->success('删除类型失败');
		}
    }
    //报事报修
    public function repairList(){
        //根据当前登录账号查询物业公司
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $page = request()->has('page') ? request()->get('page') : 1;
        $repairlist = (new repairService())->repairList($whe,$page);

        $number = count((new repairService())->repairNumber($whe));
        View::assign('number',$number);
        if(!empty($number)){
            foreach($repairlist as $lis){
                //根据小区ID查询小区名称
                $lis['community_id'] = (new repairService())->communityIdName($lis['community_id']);
                //根据报修类型ID查询报修类型
                $lis['repair_type'] = (new repairService())->typeIdName($lis['repair_type']);
                $list[] = $lis;
            }
            View::assign('list',$list);
        }else{
            View::assign('list',$repairlist);
        }
        
        View::assign('repairlist',$repairlist);
        return View::fetch('repair_list');
    }
    public function repairAdd(){
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new repairService())->communityList($whe);
        View::assign('communitylist',$communitylist);
        //查询故障类型信息
        $repairType = (new repairService())->typeList();
        View::assign('repairType',$repairType);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //查询房屋
            $data['room_address'] = $data['group'].$data['building'].'幢'.$data['unit'].'单元'.$data['room'].'室';
            $data['time_repair'] = date('Y-m-d H:i:s',time());
            $data['status'] = 2;
            unset($data['group']);
            unset($data['building']);
            unset($data['unit']);
            unset($data['room']);
            //插入数据到数据库
            $result = (new repairService())->repairAdd($data);
            if($result){
                //根据ID查询报修信息
                $repairidlist = (new repairService())->repairIdList($result);
                //根据小区ID查询打印机信息
                $communityidinfo = (new repairService())->communityIdInfo($repairidlist['community_id']);
                $machineCode = $communityidinfo['machine_code'];
                $community = $communityidinfo['name'];
                $room = $repairidlist['room_address'];
                $people = $repairidlist['people'];
                $phone = $repairidlist['phone'];
                //根据报修类型ID查询报修
                $repair_type= (new repairService())->typeIdName($repairidlist['repair_type']);
                $repairinfo = $repairidlist['repairinfo'];
                $time_thedoor = $repairidlist['time_thedoor'];
                $time_repair = $repairidlist['time_repair'];
                //$printing = (new printDemo())->repairPrinting($machineCode,$community,$room,$people,$phone,$repair_type,$repairinfo,$time_thedoor,$time_repair);
                $this->success('提交报修成功',url('repairAdd'));
            }else{
                $this->error('提交报修失败',url('repairAdd'));
            }             
        }
        return View::fetch('repair_add');
    }
    //解决报修
    public function repairSolve(){
        $id = request()->param('id');
        if(!isset($id) && empty($id)){
            $this->error('故障不存在');
        }
        //修改故障状态
        $data['id'] = $id;
        $data['status'] = 1;
        $data['time_solve'] = date('Y-m-d H:i:s',time());
        $solve = (new repairService())->repairIdSolve($data);
        if($solve){
            $this->success('解决成功');
        }else{
            $this->error('解决失败');
        }
    }

    //派工单打印
    public function repairprint(){
        $id = request()->param('id');
        $info=(new repairService())->repairIdList($id);
        //根据小区id查询物业id
        $community=Db::table("community")->where('community_id',$info["community_id"])->find();
        //根据property_id查询物业的名称
        $property=Db::table("property")->find($community["property_id"]);
        
        $series=date('YmdHis',time())."-".$info["id"];
        View::assign("series",$series);
        View::assign("property",$property);
        View::assign("info",$info);
        return View::fetch("repair_print");
    }
}